
/**
 * User: jiming
 * Date: 11-3-7
 * Time: 上午11:32
 * To change this template use File | Settings | File Templates.
 */


import groovy.text.GStringTemplateEngine
import com.google.common.primitives.Primitives

def beanName = "ContentProviderDetail";
def childBeans = ["PolicySetting"];
def beanPropertiesString = '''
	private String id;
	private String name;
	private String address;
	private String phone;
	private String description;
	private PolicySetting policySetting;

'''

// beanPropertiesString = beanPropertiesString.replaceAll("(?m)^\\s*private\\s+", "").replaceAll(";", "").replaceAll('(?m)\\s+$', "");
beanPropertiesString = beanPropertiesString.trim().replaceAll("(?m)^\\s*private\\s+", "");

beanPropertiesString = beanPropertiesString.replaceAll('(?m)^\\s*([\\w<>]+?)\\s+?(\\w+?)\\W+$', '"$2" : "$1",').trim().replaceAll(',$', '');

def binding = ["beanName": beanName
        , "beanProperties" : evaluate("[" + beanPropertiesString + "]")
        , "childBeans" : childBeans
]


def templateString = '''\
<%def StringUtils = com.springsource.util.common.StringUtils%>\
    public static $beanName new${beanName}ByInput(int i) {
        $beanName result = new $beanName();
<%for(entry in beanProperties.entrySet()) {%>\
<%if (entry.value.equals('String')) { %>\
        result.set${StringUtils.capitalize(entry.key)}("${entry.key}_" + i);
<% } else if (['int', 'Integer'].contains(entry.value)) {%>\
        result.set${StringUtils.capitalize(entry.key)}(i);
<% } else if (childBeans.contains(entry.value)) {%>\
        result.set${StringUtils.capitalize(entry.key)}(new${StringUtils.capitalize(entry.value)}ByInput(i));
<%}%>\
<%}%>\
        return result;
    }

    public static List<$beanName> new${beanName}List(int[] vars) {
        List<$beanName> result = new ArrayList<$beanName>();
        for (int i : vars) {
            ${beanName} item = new${beanName}ByInput(i);
<%childBeans.each {%>\
            item.set${StringUtils.capitalize(it)}(new${StringUtils.capitalize(it)}ByInput(i));
<%}%>\
            result.add(item);
        }
        return result;
    }

'''

def engine = new GStringTemplateEngine()
template = engine.createTemplate(templateString).make(binding)

println template.toString();

